package com.roadjava.GDOUGym.service.impl;

import com.roadjava.GDOUGym.bean.dto.CompetitionDTO;
import com.roadjava.GDOUGym.bean.dto.GameDTO;
import com.roadjava.GDOUGym.bean.entity.CompetitionDO;
import com.roadjava.GDOUGym.bean.entity.DenueHireMessageDO;
import com.roadjava.GDOUGym.bean.entity.GameDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.CompetitionConvert;
import com.roadjava.GDOUGym.convert.GameConvert;
import com.roadjava.GDOUGym.mapper.CompetitionMapper;
import com.roadjava.GDOUGym.mapper.GameMapper;
import com.roadjava.GDOUGym.service.CompetitionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CompetitionServiceImpl implements CompetitionService {
    @Resource
    private CompetitionMapper competitionMapper;

    @Resource
    private CompetitionConvert competitionConvert;

    @Override
    public ResultDTO<List<CompetitionDTO>> find(CompetitionDTO dto) {
        CompetitionDO competitionDO = competitionConvert.dto2do(dto);
        System.out.println(competitionDO);
        List<CompetitionDO> dos = competitionMapper.find(dto);
        List<CompetitionDTO> dtos = competitionConvert.dos2dtos(dos);
        long total = competitionMapper.selectCount(dto);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(CompetitionDTO competitionDTO) {
        CompetitionDTO dto = new CompetitionDTO();
        CompetitionDO competitionDO2 = competitionConvert.dto2do(competitionDTO);
        System.out.println("competitionDO2 = " + competitionDO2);

        String no;

        List<CompetitionDO> competitionDOList = competitionMapper.find(dto);
        if(competitionDOList.size() == 0){
            no = "0001";
        }else{
            String endNo = competitionDOList.get(competitionDOList.size()-1).getCompetitionNo();
            long total = Integer.parseInt(endNo)+1;

            no = total+"";
            if(total<10){
                no = "000" + no;
            }else if(total<100){
                no = "00" + no;
            }else if(total < 1000){
                no = "0" + no;
            }
        }




        // 把dto转为do对象
        CompetitionDO competitionDO = competitionConvert.dto2do(competitionDTO);
        competitionDO.setCompetitionNo(no);
        int result = competitionMapper.add(competitionDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("添加成功");
        }else {
            return ResultDTO.buildSuccess("添加失败");
        }
    }

    @Override
    public ResultDTO<String> change(CompetitionDTO competitionDTO) {
        // 把dto转为do对象
        CompetitionDO competitionDO = competitionConvert.dto2do(competitionDTO);
        int result = competitionMapper.change(competitionDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(CompetitionDTO competitionDTO) {
        // 把dto转为do对象
        CompetitionDO competitionDO = competitionConvert.dto2do(competitionDTO);
        int result = competitionMapper.delete(competitionDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("删除成功");
        }else {
            return ResultDTO.buildSuccess("删除失败");
        }
    }
}
